package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.system.domain.vo.SensorVo;
import org.dromara.system.mapper.SensorMapper;
import org.dromara.system.service.ISensorService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 设备信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-10-21
 */
@RequiredArgsConstructor
@Service
public class SensorServiceImpl implements ISensorService {
    private final SensorMapper baseMapper;
    DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Override
    public void insertData(float value, Long deviceId) {
        String tableName = "sensor_" + deviceId;
        LocalDateTime now = LocalDateTime.now();
        Timestamp timestamp = Timestamp.from(now.toInstant(ZoneOffset.UTC));
        baseMapper.insertData(tableName, value, deviceId, timestamp);
    }

    @Override
    public void insertPointData(BigDecimal value, Long deviceId) {
        String tableName = "sensor_" + deviceId;
        LocalDateTime now = LocalDateTime.now();
        Timestamp timestamp = Timestamp.from(now.toInstant(ZoneOffset.UTC));
        baseMapper.insertPointData(tableName, value, deviceId, timestamp);
    }

    @Override
    public Float getLatestValue(Long deviceId) {
        String tableName = "sensor_" + deviceId;
        return baseMapper.getLatestValueByDeviceId(tableName);
    }

    @Override
    public BigDecimal getLatestData(Long deviceId) {
        String tableName = "sensor_" + deviceId;
        return baseMapper.getLatestData(tableName);
    }

    @Override
    public List<SensorVo> selectSensorList(Long deviceId) {
        String tableName = "sensor_" + deviceId;
        return baseMapper.selectSensorList(tableName);
    }

    @Override
    public IPage<SensorVo> getPagedDataByDeviceId(Long deviceId, PageQuery pageQuery, String startTime, String endTime) {
        Page<SensorVo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        LocalDateTime startTime1 = null;
        LocalDateTime endTime1 = null;
        if (startTime != null && !startTime.isBlank() && endTime != null && !endTime.isBlank()) {
            LocalDate startDate = LocalDate.parse(startTime, DATE_TIME_FORMATTER);
            startTime1 = startDate.atStartOfDay();
            LocalDate endDate = LocalDate.parse(endTime, DATE_TIME_FORMATTER);
            endTime1 = endDate.atTime(23, 59, 59);
        }
        // 将 startTime1 和 endTime1 转换为 YYYY-MM-DD HH:MM:SS 格式的字符串
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        String startTimeStr = startTime1 != null ? startTime1.format(outputFormatter) : null;
        String endTimeStr = endTime1 != null ? endTime1.format(outputFormatter) : null;
        String tableName = "sensor_" + deviceId;
        return baseMapper.getPagedDataByDeviceId(page, tableName, startTimeStr, endTimeStr);
    }

    @Override
    public Long selectCountReportData() {
        return baseMapper.selectCountReportData();
    }



    @Override
    public Long selectYeasCount() {
        return baseMapper.selectCountReportData();
    }

    @Override
    public Long selectTotalCount(List<Long> allSubNodeIds) {
        return baseMapper.selectTotalCount(allSubNodeIds);
    }

    @Override
    public Long getYtDataCount() {
        return baseMapper.getYtDataCount();
    }

    @Override
    public Long getDhmDataCount() {
        return baseMapper.getDhmDataCount();
    }
    @Override
    public Long getFiveDataCount() {
        return baseMapper.getFiveDataCount();
    }


}
